<template>
  <div class="mod-unit">
    <el-form :inline="true" size="small" :model="dataForm">
      <el-form-item>
        <el-input v-model="dataForm.name" placeholder="单位名称" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-button @click="query()">查询</el-button>
        <el-button type="primary" @click="addOrUpdateHandle()">新增</el-button>
        <!-- 删除没做 -->
        <el-button
          type="danger"
          :disabled="dataListSelections.length <= 0"
          @click="deleteHandle()"
        >批量删除</el-button>
      </el-form-item>
    </el-form>

    <el-table
      :data="dataList"
      size="mini"
      row-key="id"
      border
      v-loading="dataListLoading"
      @selection-change="selectionChangeHandle"
      style="width: 100%"
    >
      <el-table-column type="selection" header-align="center" align="center" width="50"></el-table-column>
      <el-table-column type="index" align="center" label="编号" width="100" />
      <el-table-column prop="name" header-align="center" min-width="150" align="left" label="属性名称"></el-table-column>

      <el-table-column
        prop="code"
        header-align="center"
        min-width="150"
        align="center"
        label="属性编码"
      ></el-table-column>

      <el-table-column
        prop="dbFieldType"
        header-align="center"
        min-width="150"
        align="center"
        label="数据库类型"
      ></el-table-column>

      <el-table-column prop="dataState" header-align="center" align="center" label="状态">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.dataState === -1" size="small" type="danger">禁用</el-tag>
          <el-tag v-else size="small">正常</el-tag>
        </template>
      </el-table-column>

      <el-table-column prop="orderNum" header-align="center" align="center" label="排序号"></el-table-column>
      <el-table-column
        prop="remark"
        header-align="center"
        align="center"
        width="200"
        :show-overflow-tooltip="true"
        label="备注"
      ></el-table-column>

      <el-table-column
        prop="createTime"
        header-align="center"
        align="center"
        width="180"
        label="创建时间"
      ></el-table-column>

      <el-table-column fixed="right" header-align="center" align="center" width="200" label="操作">
        <template slot-scope="scope">
          <!-- <el-button v-if="isAuth('sys:menu:update')" type="text" size="small" @click="addOrUpdateHandle(scope.row.id)">修改</el-button> -->
          <!-- <el-button v-if="isAuth('sys:menu:delete')" type="text" size="small" @click="deleteHandle(scope.row.id)">删除</el-button> -->

          <el-button type="text" size="small" @click="addOrUpdateHandle(scope.row, 1)">添加下级</el-button>
          <el-button type="text" size="small" @click="addOrUpdateHandle(scope.row, 2)">修改</el-button>
          <el-button type="text" size="small" @click="deleteHandle(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 弹窗, 新增 / 修改 -->
    <property-add-or-update
      v-if="addOrUpdateVisible"
      ref="propertyAddOrUpdate"
      @refreshDataList="getDataList"
    ></property-add-or-update>
  </div>
</template>


<script>
import PropertyAddOrUpdate from "./property-add-or-update";
import { pageFieldProperty, removeFieldProperty } from "@/api/table";
import { treeDataTranslate } from "@/utils";

export default {
  data() {
    return {
      dataForm: {
        name: ""
      },
      dataList: [],
      dataListLoading: false,
      addOrUpdateVisible: false,
      dataListSelections: []
    };
  },
  components: {
    PropertyAddOrUpdate
  },
  activated() {
    this.getDataList();
  },
  methods: {
    query() {
      this.getDataList();
    },
    // 获取数据列表
    getDataList() {
      this.dataListLoading = true;
      pageFieldProperty({ name: this.dataForm.name }).then(res => {
        if (res && res.code === 0) {
          this.dataList = treeDataTranslate(res.data, "id");
        }
        this.dataListLoading = false;
      });
    },
    // 多选
    selectionChangeHandle(val) {
      this.dataListSelections = val;
    },
    // 新增 / 修改
    addOrUpdateHandle(data, addChildren) {
      this.addOrUpdateVisible = true;
      if (addChildren) {
        data.addChildren = addChildren;
      }
      this.$nextTick(() => {
        this.$refs.propertyAddOrUpdate.init(data);
      });
    },
    // 删除
    deleteHandle(res) {
       var ids = res
        ? [res.id]
        : this.dataListSelections.map((item) => {
            return item.id;
          });
      this.$confirm(`确定对[${res.name}]进行[删除]操作?`, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          removeFieldProperty({ id: `${res.id}` }).then(res => {
            if (res && res.code === 0) {
              this.$message({
                message: "操作成功",
                type: "success",
                duration: 1000,
                onClose: () => {
                  this.getDataList();
                }
              });
            } else {
              this.$message.error(data.msg);
            }
          });
        })
        .catch(() => {});
    }
  }
};
</script>